{% extends 'base.html' %}

{% block title %}用户管理{% endblock %}

{% block content %}
<div class="card">
    <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
        <h4 class="mb-0">用户管理</h4>
    </div>
    <div class="card-body">
        <!-- 搜索表单 -->
        <form method="get" class="mb-4">
            <div class="row g-3">
                <div class="col-md-4">
                    <input type="text" class="form-control" name="search" value="{{ search }}" placeholder="搜索用户名...">
                </div>
                <div class="col-md-3">
                    <select class="form-select" name="user_type">
                        <option value="">所有用户类型</option>
                        {% for type_key, type_name in user_types.items() %}
                        <option value="{{ type_key }}" {% if user_type == type_key %}selected{% endif %}>
                            {{ type_name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">搜索</button>
                </div>
                <div class="col-md-2">
                    <a href="{{ url_for('admin.users') }}" class="btn btn-secondary w-100">重置</a>
                </div>
            </div>
        </form>

        <!-- 用户列表表格 -->
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户名</th>
                        <th>用户类型</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for user in pagination.items %}
                    <tr>
                        <td>{{ user.id }}</td>
                        <td>{{ user.username }}</td>
                        <td>{{ user.get_user_type_display() }}</td>
                        <td>
                            {% if user.user_type != 'admin' %}
                            <form action="{{ url_for('admin.delete_user', id=user.id) }}" method="post" class="d-inline">
                                <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('确定要删除此用户吗？')">
                                    删除
                                </button>
                            </form>
                            {% endif %}
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <!-- 分页导航 -->
        {% if pagination.pages > 1 %}
        <nav aria-label="Page navigation" class="mt-4">
            <ul class="pagination justify-content-center">
                <!-- 上一页 -->
                <li class="page-item {% if not pagination.has_prev %}disabled{% endif %}">
                    <a class="page-link" href="{% if pagination.has_prev %}{{ url_for('admin.users', page=pagination.prev_num, search=search, user_type=user_type) }}{% else %}#{% endif %}">
                        上一页
                    </a>
                </li>
                
                <!-- 页码 -->
                {% for page in pagination.iter_pages(left_edge=2, left_current=2, right_current=3, right_edge=2) %}
                    {% if page %}
                        <li class="page-item {% if page == pagination.page %}active{% endif %}">
                            <a class="page-link" href="{{ url_for('admin.users', page=page, search=search, user_type=user_type) }}">
                                {{ page }}
                            </a>
                        </li>
                    {% else %}
                        <li class="page-item disabled">
                            <span class="page-link">...</span>
                        </li>
                    {% endif %}
                {% endfor %}
                
                <!-- 下一页 -->
                <li class="page-item {% if not pagination.has_next %}disabled{% endif %}">
                    <a class="page-link" href="{% if pagination.has_next %}{{ url_for('admin.users', page=pagination.next_num, search=search, user_type=user_type) }}{% else %}#{% endif %}">
                        下一页
                    </a>
                </li>
            </ul>
        </nav>
        {% endif %}
    </div>
</div>
{% endblock %} 